#!/usr/local/bin/php
<?php
require_once 'common.php';

/* @var $logger Log */
global $logger;
/* @var $userMigrationTable OpTableModel */
global $userMigrationTable;
/* @var $developerTable OpTableModel */
global $developerTable;
/* @var $companyTable OpTableModel */
global $companyTable;
/* @var $companyDeveloperTable OpTableModel */
global $companyDeveloperTable;
/* @var $appTable OpTableModel */
global $appTable;
/* @var $appConfigTable OpTableModel */
global $appConfigTable;
/* @var $appDeveloperTable OpTableModel */
global $appDeveloperTable;

$logger->setMask(Log::MAX(PEAR_LOG_INFO));

$options = getopt('a:f:g:u:r:');
if (empty($options)) {
    $logger->err("usage: " . basename(__FILE__) . " [-a add|delete] [-f member_list_file|-g app_id -u pt] [-r role]");
    exit();
}
if (isset($options['a'])) {
    $action = $options['a'];
} else {
    $action = 'add';
}
if (!in_array($action, array('add', 'delete'))) {
    $logger->err("wrong action");
    exit();
}
if (isset($options['f'])) {
    $contents = file_get_contents($options['f']);
    if ($contents === false) {
        $logger->err("get file contents failed");
        exit();
    }
} else if (isset($options['g']) && isset($options['u'])) {
    $contents = $options['g'] . "\t" . $options['u'];
} else {
    $logger->err("no user specified");
    exit();
}
if (isset($options['r'])) {
    $role = OaAuthorityModel::getConstantValueByName($options['r']);
    if ($role === 0) {
        $logger->err("wrong role");
        exit();
    }
} else {
    $role = OaAuthorityModel::ROLE_APP_DEVELOPER;
}

$appModel = new OaAppModel(OaAppModel::APP_CONFIG_ENV_PRODUCTION, $logger);

$rows = explode("\n", trim($contents));
if ($action == 'add') {
    foreach ($rows as $row) {
        $row = explode("\t", trim($row));
        $companyId = isset($row[0]) ? intval($row[0]) : 0;
        if ($companyId == 0) {
            $logger->warning("app id invalid: app_id-$companyId");
            continue;
        }
        $pt = isset($row[1]) ? $row[1] : '';
        if ($pt == '') {
            $logger->warning("user invalid: pt-$pt");
            continue;
        }
        
        if (!$appModel->addDeveloper($companyId, $pt, $role)) {
            $logger->err("add app developer failed: app_id-$companyId pt-$pt error-" . $appModel->getErrorMessage());
            continue;
        }
                
        $logger->info("add app member succeed: app_id-$companyId pt=$pt");
    }
} else if ($action == 'delete') {
    foreach ($rows as $row) {
        $row = explode("\t", trim($row));
        $companyId = isset($row[0]) ? intval($row[0]) : 0;
        if ($companyId == 0) {
            $logger->warning("app id invalid: app_id-$companyId");
            continue;
        }
        $pt = isset($row[1]) ? $row[1] : '';
        if ($pt == '') {
            $logger->warning("user invalid: pt-$pt");
            continue;
        }
        
        if (!$appModel->deleteDeveloperByPt($companyId, $pt)) {
            $logger->err("delete app developer failed: app_id-$companyId pt-$pt");
            continue;
        }
        
        $logger->info("delete app member succeed: app_id-$companyId pt=$pt");
    }
}
